<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="windows-1252"%>

<%@ page import="javax.naming.*, edu.ubb.bibliospring.backend.model.Author, edu.ubb.bibliospring.backend.service.AuthorService, java.util.List, javax.ejb.EJBAccessException" %>


<%!
    AuthorService authorService = null;

    public void jspInit() {
        try {
            InitialContext context = new InitialContext();
            authorService = (AuthorService) context.lookup("ejb/AuthorService");
        } catch (NamingException exception) {
            exception.printStackTrace();
        }
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>Authors</title>
    </head>
    <body>
        <h2>Authors in database</h2>
        <table  border="1" >
            <%
                List<Author> authors = new ArrayList<Author>();
                try {
                    authors = authorService.getAll();
            %>
            <tr style="background-color:#66FFCC">
                <% if (request.isUserInRole("super")) { %>
                <td>Id</td>
                <%}%>
                <td>First name</td>
                <td>Last name</td>
            </tr>    
            <%
            } catch (EJBAccessException e) { %>
            <h2 style="color:red" >UNAUTHORIZED USER<h2>
                    <%}
                        for (Author author : authors) {
                    %>
                    <tr>
                        <% if (request.isUserInRole("super")) {%>
                        <td>
                            <%= author.getId()%>
                        </td>
                        <%}%>
                        <td>
                            <%= author.getFirstName()%>
                        </td>
                        <td>
                            <%= author.getLastName()%>
                        </td>
                    </tr>
                    <%}%>
                    </table>

                    <% if (request.isUserInRole("user")) { %>
                    <p>This advertisement shows only for simple users</p>
                    <%}%> 

                    <a href="Logout" >Logout </a>
                    </body>
                    </html>
